package br.com.wepa.webapps.orca.visual.client;

import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.RootPanel;



/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Menu implements EntryPoint {

	private static final boolean VERTICAL = true;
	private static final String MENU = "menuBar";	
	
  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
    

    // (1) Create the client proxy. Note that although you are creating the
    // service interface proper, you cast the result to the asynchronous
    // version of
    // the interface. The cast is always safe because the generated proxy
    // implements the asynchronous interface automatically.
    //
    MenuServiceAsync menuService = (MenuServiceAsync) GWT.create(MenuService.class);

    // (2) Specify the URL at which our service implementation is running.
    // Note that the target URL must reside on the same domain and port from
    // which the host page was served.
    //
    ServiceDefTarget endpoint = (ServiceDefTarget) menuService;
    String moduleRelativeURL = GWT.getModuleBaseURL() + "menu";  
    endpoint.setServiceEntryPoint(moduleRelativeURL);

    // (3) Create an asynchronous callback to handle the result.
    //

    AsyncCallback callback = new AsyncCallback() {
      public void onSuccess(Object result) {
        // do some UI stuff to show success
    	 MenuBar m = montaMenu(new MenuBar(), (List)result);
    	 m.setStyleName(GWT.getModuleBaseURL()+"menu.css");
    	 RootPanel.get(MENU).add(m);   	 
    	  
      }

      public void onFailure(Throwable caught) {
        // do some UI stuff to show failure	      	 
    	  String msg = caught.getMessage();
    	  if(msg == null || msg.trim().equals("")){
    		  msg =  caught.toString();
    	  }
    	  Window.alert(ClientConstants.ERROR_MONTAGEM_MENU +" \n ERRO: \n"+ msg);    	  
      }
    };

    // (4) Make the call. WebControlThreads flow will continue immediately and later
    // 'callback' will be invoked when the RPC completes.
    //
    
    menuService.getMenu(callback);
    

  }
  
  public MenuBar montaMenu(MenuBar menu, List filhos) {	  
		for (int i = 0; i < filhos.size(); i++) {
			final MenuItemVisual item = (MenuItemVisual) filhos.get(i);			
			if (item.getFilhos().isEmpty()) {
				Command cmd = new Command() {
					public void execute() {
						doGet(item.getAction());		
						
					}
				};
				
				menu.addItem(":: " + item.getLabel(), cmd);
				
			} else {
				menu.addItem(":: " +item.getLabel(),true, montaMenu(new MenuBar(VERTICAL),
						item.getFilhos()));
			}
		}
		return menu;
	}
  
  
  	public static void doGet(String url) {
  		Window.open(url, "_self","");
	 }
  	
}
